import re
import json
import random

from lxml import etree
import requests as rs
from z_web.models import *

save_url = "https://www.dwang.top/n/"

text_list = [
    '<a title="一部电影网主页" href="https://www.dwang.top/">电影推荐</a>',
    '<a title="一部电影网主页" href="https://www.dwang.top/">经典电影</a>',
    '<a title="一部电影网主页" href="https://www.dwang.top/">高分电影</a>',
    '<a title="误杀瞒天记" href="https://www.dwang.top/movie/13/">误杀瞒天记</a>',
    '<a title="误杀" href="https://www.dwang.top/movie/14/">误杀</a>',
    '<a title="一部电影网主页" href="https://www.dwang.top/">一部电影网</a>',
]

def get_an_text():
    # 选择一个插入的内容
    slt = random.choice(text_list)
    return slt.format(random.choice(Movie.objects.values_list("id", flat=True))) if "{}" in slt else slt

def insert_content(content):
    # 给content随机插入一些关键词
    content_list = content.split("，")
    content_length = len(content_list)
    for c in range(content_length):
        if random.random() > 0.5:
            content_list.insert(content_length-c, get_an_text())
    return "，".join(content_list)

def save_new(title, content, source):
    try:
        mn, create_now = MovieNew.objects.get_or_create(title=title, source=source)
        if create_now:
            mn.content = insert_content(content)
            mn.save()
    except:
        pass

def ifeng_ent():
    # 凤凰网电影板块爬虫
    res = rs.get("http://ent.ifeng.com/movie/")
    text = json.loads(re.findall("allData = (.*);", res.text)[0])["newsstream"]
    for i in text:
        title = i["title"]
        res = rs.get(i["url"])
        html = etree.HTML(res.text)
        content = html.xpath('//div[@class="text-3w2e3DBc"]')[0]
        content = etree.tostring(content, encoding='utf-8').decode('utf-8')
        source = html.xpath("//span[@class='source-qK4Su0--']/a/text()")[0]
        # res = rs.post(save_url, data={
        #     "title": title,
        #     "content": content,
        #     "source": source,
        # })
        # MovieNew.objects.get_or_create(title=title, content=insert_content(content), source=source)
        save_new(title, content, source)


def neteast_net():
    # 163 电影板块爬虫
    res = rs.get("https://ent.163.com/special/000381Q1/newsdata_movieidx.js?callback=data_callback")
    text = json.loads(res.text[14:-1])
    for i in text:
        try:
            title = i["title"]
            url = i["docurl"]
            res = rs.get(url)
            html = etree.HTML(res.text)
            div = html.xpath('//div[@class="post_body"]')[0]
            content = etree.tostring(div, encoding='utf-8').decode("utf-8")
            source = html.xpath('//div[@class="post_info"]/text()')[0].split(":")[-1].strip()
            # MovieNew.objects.get_or_create(title=title, content=insert_content(content), source=source)
            save_new(title, content, source)
        except:
            pass


if __name__ == '__main__':
    neteast_net()
    